 <?php 
ob_start();
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		
		$this->data = array();
		$this->data['user_detail'] = $this->session->all_userdata();
		
		if(!$_SERVER['PHP_SELF'] == '/~exelexio/mi5/index.php/register'){
			if(!$this->session->userdata('user_id') && $this->session->userdata('user_id') =='')
			{
				$this->session->sess_destroy();
				header('Location: '.BASE_URL.'/login');
				redirect(BASE_URL.'/login');
				exit();
			}
		}
		#load all database and required models
		$this->load->database();
		$this->load->model('common_model');
		$this->load->model('user_model');
	}
	
	public function dashboard(){
		$this->data['result'] = $this->user_model->getinfo();
		$this->data['query'] = $this->user_model->getawards();
		$this->data['user_award'] = $this->user_model->getUserAward($this->data['user_detail']['user_id']);
		$this->data['user_award_data'] = $this->user_model->getUserAward($this->data['user_detail']['user_id'], 'All');
		$this->load->view('user/dashboard',$this->data);
	}
	
	public function edit_details(){
		
		if(isset($_POST['submit'])){
			$this->load->library('form_validation');
			if($this->input->post('npassword') ||$this->input->post('cpassword')){
				$this->form_validation->set_rules('npassword','Password','required|trim|');
				$this->form_validation->set_rules('cpassword','Confirm Password','required|trim|matches[npassword]');	
			}
			$this->form_validation->set_rules('fname','First Name','required|trim');
			$this->form_validation->set_rules('lname','Last Name','required|trim');
			if($this->form_validation->run()) {
				$this->load->helper(array('form','url'));
				$config['upload_path'] = 'files/applicant_photo/';
				$config['allowed_types'] = 'jpg|jpeg|png';
				$config['overwrite'] = TRUE;
				$config['max_size']	='2048';
				$config['file_name'] = 'image_user_'.$this->session->userdata('user_id');
				$this->load->library('upload', $config);
				if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])){
					if (!$this->upload->do_upload()) {
						$data['error'] = $this->upload->display_errors();
						$data['result']=$this->user_model->getinfo();
						$this->load->view('user/edit_details',$data);
						return false;
					}
				}
				if($this->user_model->edit_details()){
					$this->session->set_userdata('update_message','Profile updated sucessfully');
				}
				else {
					$this->session->set_usertdata('update_message','There was some problem in updating your profile, please try again later');
				}
			}
		}
		$data['query'] = $this->user_model->getcountry();
		$data['result']=$this->user_model->getinfo();
		$this->load->view('user/edit_details',$data);
	}
	
	public function research_background(){
		$data['result']=$this->user_model->getinfo();
		$this->load->view('user/research_background.php',$data);
		
	}
	
	public function register(){
		if(($this->session->userdata('application_event_id')) &&($this->session->userdata('application_award_id'))){
			if(isset($_POST['fname'])){
	///			var_dump($this->session->userdata('application_event_id')); die();
				
				$this->form_validation->set_rules('fname','First Name','required|xss_clean');
				$this->form_validation->set_rules('lastname','Last Name','required|xss_clean');
				$this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[scopus_user.user_email]|xss_clean');
				$this->form_validation->set_rules('password','Password','required|trim');
				$this->form_validation->set_rules('cpassword','Confirm Password','required|trim');
				$this->form_validation->set_message('matches',"Password / Confirm password must be same");
				$this->form_validation->set_message('is_unique',"You are already Registered");
				if($this->form_validation->run()) {
					//generate a random key
					$activatekey= md5(uniqid());
					if($this->user_model->registration($activatekey)){
						//send a email to user
						$config['protocol']='smtp';
						$config['smtp_host']='smtpout.asia.secureserver.net';
						$config['smtp_port']='80';
						$config['smtp_timeout']='30';
						$config['smtp_user']='info.elsevier@logiquebraincreations.com';
						$config['smtp_pass']='default@123';
						$config['charset']='utf-8';
						$config['newline']="\r\n";
						$config['wordwrap'] = TRUE;
						$config['mailtype'] = 'html';
						$this->email->initialize($config);
						$this->email->from('no-reply@elsevier.com', 'Elsevier');
						$this->email->to($this->input->post('email'));
						$this->email->subject('Elsevier - Account Activation');
						$message='<!DOCTYPE html>
										<html>
										<head>
										<title>Elsevier</title>
										<meta charset="utf-8" />
										</head>
										
										<body style="margin:0px; padding:0px;">
											<table width="640" border="0" align="center" cellpadding="0" cellspacing="0"  style="background-color:#fafafa; border:1px solid #e1e1e1;">
												<tr bgcolor="#FF8200">
													<td>
														<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
															<tr>
																<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
															<tr>
																<td bgcolor="#ff8200" style="font-family:Calibri; font-size:16px; color:#000000;"><a href="http://www.elsevier.com/" target="_blank" style="text-decoration:none"><img src="'.ASSET_URL.'img/logo.png" border="0" alt="Elsevier" style="display:block;" /></a></td>
															</tr>
															<tr>
																<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
														</table>
													</td>
												</tr>
												<tr>
													<td>
														<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:#fafafa; margin:0 auto;">
															<tr>
																<td bgcolor="#fafafa">
																	<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
																		<tr>
																			<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
																		</tr>
																		<tr>
																			<td bgcolor="#FAFAFA" style="font-family:Calibri; font-size:16px; font-weight:bold; color:#333;">Dear '.ucwords($this->input->post('fname')).',</td>
																		</tr>
																	</table>
																</td>
															</tr>
															<tr>
																<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
															<tr>
																<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333;">You have sucessfully registered for Scopus Awards. Please click on the link below to activate your account.</td>
															</tr>
															<tr>
																<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
															<tr>
																<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;"><a href="'.BASE_URL.'/users/activate_user?key='.$activatekey.'&award_id='.base64_encode($this->session->userdata('application_award_id')).'&event_id='.base64_encode($this->session->userdata('application_event_id')).'" target="_blank" style="text-decoration:none; color:#ff8200;">Click here to activate your account</a></td>
															</tr>
															<tr>
																<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
															<tr>
																<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;">Regards,<br />
										Elsevier Awards</td>
															</tr>
															<tr>
																<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
															</tr>
														</table>
													</td>
												</tr>
												<tr>
													<td bgcolor="#FFFFFF">
														<table width="640" border="0" align="center" cellpadding="0" cellspacing="0" style="vertical-align:top;">
															<tr>
																<td bgcolor="#232933">
																	<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
																		<tr>
																			<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
																		</tr>
																		<tr>
																			<td valign="bottom" bgcolor="#232933" style="text-align:center;"><span style="font-size: 12px; font-family: Calibri; line-height: 17px; color: #ffffff; text-align: center;">Copyright &copy; 2015 Elsevier. All Rights Reserved </span></td>
																		</tr>
																		<tr>
																			<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
																		</tr>
																	</table>
																</td>
															</tr>
														</table>
													</td>
												</tr>
											</table>
										</body>
										</html>';
						$this->email->message($message);
						if($this->email->send()) {
							$this->session->set_userdata('message','Registered Sucessfully. An activation link has been mailed to your registered email id');
							header('Location: '.BASE_URL.'/'.'register');
							redirect(BASE_URL.'/'.'register');
							exit();	
						}
						else {
							$this->session->set_userdata('message','There was a problem in seding the mail. Please contact the administrator');
							header('Location: '.BASE_URL.'/'.'register');
							redirect(BASE_URL.'/'.'register');
							exit();
						}
					}
					else {
						$this->session->set_userdata('message','We encountered some problem. Please try again later');
						header('Location: '.BASE_URL.'/'.'register');
						redirect(BASE_URL.'/'.'register');
						exit();
					}	
				}
			}
			$data['query'] = $this->user_model->getcountry();
			$this->load->view('user/register',$data);
		}
	else {
		header('Location: '.BASE_URL.'/'.'login');
			redirect(BASE_URL.'/'.'login');
			exit();
	}
	}
	
	public function condiotion_check(){
		if($this->input->post('conditions')==''){
			return false;
		}
		else {
			return true;
		}
	}
		
	public function activate_user(){
		if($this->input->get('key')){
				$event_id = base64_decode($this->input->get('event_id'));
				$award_id = base64_decode($this->input->get('award_id'));
				$key = htmlspecialchars(strip_tags($this->input->get('key')),ENT_QUOTES);
				$data['result'] = $this->user_model->activate_user($key);
				if($data['result']!=NULL){
					$name = $data['result'][0]->user_first_name.' ';
					if($data['result'][0]->user_middle_name!=' '){
						$name .= $data['result'][0]->user_middle_name.' ';
					}
					$name .= $data['result'][0]->user_last_name.' ';
					$email = $data['result'][0]->user_email;
					//send a email to user
					$config['protocol']='smtp';
			        $config['smtp_host']='smtpout.asia.secureserver.net';
			        $config['smtp_port']='80';
			        $config['smtp_timeout']='30';
			        $config['smtp_user']='info.elsevier@logiquebraincreations.com';
			        $config['smtp_pass']='default@123';
			        $config['charset']='utf-8';
			        $config['newline']="\r\n";
			        $config['wordwrap'] = TRUE;
			        $config['mailtype'] = 'html';
			        $this->email->initialize($config);
			        $this->email->from('no-reply@elsevier.com', 'Elsevier');
			        $this->email->to($email);
			        $this->email->subject('Elsevier - Thank you for activating your account');
			        $message='<!DOCTYPE html>
						<html>
							<head>
							<title>Elsevier</title>
							<meta charset="utf-8" />
							</head>
							
							<body style="margin:0px; padding:0px;">
							<table width="640" border="0" align="center" cellpadding="0" cellspacing="0"  style="background-color:#fafafa; border:1px solid #e1e1e1;">
							<tr bgcolor="#FF8200">
							<td><table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
							<tr>
							<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#ff8200" style="font-family:Calibri; font-size:16px; color:#000000;"><a href="http://www.elsevier.co.in" target="_blank" style="text-decoration:none"><img src="'.ASSET_URL.'img/logo.png" border="0" alt="Elsevier" style="display:block;" /></a></td>
							</tr>
							<tr>
							<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							</table></td>
							</tr>
							<tr>
							<td>
							<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:#fafafa; margin:0 auto;">
							
							<tr>
							<td bgcolor="#fafafa">
							<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
							<tr>
							<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#FAFAFA" style="font-family:Calibri; font-size:16px; font-weight:bold; color:#333;">Dear '.ucwords($name).',</td>
							</tr>
							</table>
							</td>
							</tr>
							<tr>
							<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333;">You have successfully activated your account</td>
							</tr>
							<tr>
							<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;">Please click on the link below to login to your account.</td>
							</tr>
							<tr>
							<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;"><a href="'.BASE_URL.'/login" target="_blank" style="text-decoration:none; color:#ff8200;">Click here to login</a></td>
							</tr>
							<tr>
							<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							<tr>
							<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;">Regards,<br />
							Elsevier Awards</td>
							</tr>
							<tr>
							<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
							</tr>
							</table>
							
							</td>
							</tr>
							<tr>
							<td bgcolor="#FFFFFF"><table width="640" border="0" align="center" cellpadding="0" cellspacing="0" style="vertical-align:top;">
							<tr>
							<td bgcolor="#232933"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
							<tr>
							<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
							</tr>
							<tr>
							<td valign="bottom" bgcolor="#232933" style="text-align:center;"><span style="font-size: 12px; font-family: Calibri; line-height: 17px; color: #ffffff; text-align: center;">Copyright &copy; 2015 Elsevier. All Rights Reserved </span></td>
							</tr>
							<tr>
							<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
							</tr>
							</table></td>
							</tr>
							</table></td>
							</tr>
							</table>
							</body>
						</html>';
			        $this->email->message($message);
					$this->session->set_userdata('activation_success', 'Thank you for activating your account');
					$this->session->userdata('application_event_id',$event_id);
					$this->session->userdata('application_award_id',$award_id);
					header('Location: '.BASE_URL.'/'.'login');
					redirect(BASE_URL.'/'.'login');
					exit();
				}
				else {
					$this->session->set_userdata('activation_unsuccess', 'Invalid activation key');
					header('Location: '.BASE_URL.'/'.'login');
					redirect(BASE_URL.'/'.'login');
					exit();
				}
		}else {
			header('Location: '.BASE_URL.'/'.'login');
			redirect(BASE_URL.'/'.'login');
			exit();
		}
	}
	
	public function refree(){
		if($this->input->get('key')){
			$key = base64_decode($this->input->get('key'));
			$data['refree_key_check'] = $this->user_model->checkKey_refree($key);
			if($data['refree_key_check'] == NULL){
				header('Location: '.BASE_URL.'/'.'login');
				exit();
			}else {
				$applicant_id = $data['refree_key_check'][0]->user_application_refree_applicant_id; 
				$data['applicant_details1'] = $this->user_model->getInfo_Applicant($applicant_id);
				if($data['applicant_details1'][0]->user_application_field5!=NULL){
					$data['applicant_country'] = $this->user_model->getcountryById($data['applicant_details1'][0]->user_application_field5);
				}
				$data['applicant_details2'] = $this->user_model->getInfo_App($applicant_id);
			}
		
		}
		else {
			header('Location: '.BASE_URL.'/'.'login');
			exit();
			
		}
		if(isset($_POST['name'])){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('name','Name','required|xss_clean');
			$this->form_validation->set_rules('email','Email','required|valid_email|xss_clean');
			$this->form_validation->set_rules('phone','Phone','required|xss_clean');
			$this->form_validation->set_rules('state','State','required|xss_clean');
			$this->form_validation->set_rules('zip_code','Postal Code','required|xss_clean');
			$this->form_validation->set_rules('position','Position','required|xss_clean');
			$this->form_validation->set_rules('affiliation','Affiliation','required|xss_clean');
			$this->form_validation->set_rules('Address1','Address','required|xss_clean');
			$this->form_validation->set_rules('Address2','Address','xss_clean');
			$this->form_validation->set_rules('Address3','Address','xss_clean');
			$this->form_validation->set_rules('Comments1','Answer 1','required|xss_clean');
			$this->form_validation->set_rules('Comments2','Answer 2','required|xss_clean');
			$this->form_validation->set_rules('Comments3','Answer 3','required|xss_clean');
			$this->form_validation->set_rules('Comments4','Answer 4','required|xss_clean');
			$this->form_validation->set_rules('Comments5','Answer 5','required|xss_clean');
			$this->form_validation->set_rules('Comments6','Answer 6','required|xss_clean');
			$this->form_validation->set_rules('Comments7','Answer 7','required|xss_clean');
			if($this->form_validation->run()) {

				if($this->user_model->refree_details()){
				}
			}
		}
		$data['country'] = $this->user_model->getcountry();
		$this->load->view('refree/refree',$data);
	}
	
	public function redirectApplicationForm()
	{
		if($this->input->get('formID') && $this->input->get('type') && $this->input->get('type') == 'new')
		{
			$form = htmlspecialchars(strip_tags($this->input->get('formID')),ENT_QUOTES);
			$type = htmlspecialchars(strip_tags($this->input->get('type')),ENT_QUOTES);
			header('Location: '.BASE_URL.'/user/application_new/'.$form);
			redirect(BASE_URL.'/user/application_new/'.$form);
			exit();
		}
		else {
			header('Location: '.BASE_URL.'/user/dashboard');
			redirect(BASE_URL.'/user/dashboard');
			exit();
		}
	}
	
	public function applicationNew($value='')
	{
		if($this->input->get('event') && $this->input->get('award')){
			$application_event_id = base64_decode($this->input->get('event'));
			$application_award_id = base64_decode($this->input->get('award'));
			$this->session->set_userdata('application_event_id', $event_id);
			$this->session->set_userdata('application_award_id', $award_id);
		}
		if($this->session->userdata('user_id') && $this->session->userdata('user_id') != '' && $this->session->userdata('user_logedin'))
		{
			$data['user_data'] = $this->user_model->get_userAppdata();
			$data['user_data_refree'] = $this->user_model->get_userRefreedata();
			$data['result']=$this->user_model->getinfo();
			if($this->session->userdata('application_event_id')){
				$data['query']=$this->user_model->getCurrentAwards();
				
			}
			$data['country'] = $this->common_model->getAllCountry();
			$data['gender'] = $this->common_model->getGender();
			$data['form_list'] = $this->user_model->getFormList($value, 'Applicant Particulars');
			$data['form_list_edu'] = $this->user_model->getFormList($value, 'Educational Background');
			$data['form_list_doc'] = $this->user_model->getFormList($value, 'Document');
			$data['form_detail'] = $this->user_model->getFormData($value);
			$data['form_submit'] = $this->user_model->checkFormSubmit($value, $this->session->userdata['user_id']);
			if($data['form_list'] && $data['form_list'] != NULL)
			{
				if(!$this->session->userdata('user_form_next'))
				{
					$this->session->set_userdata('user_form_next1', 'personal');
				}
				$this->load->view('user/application_form',$data);
			}
			else {
				header('Location: '.BASE_URL.'/user/dashboard');
				redirect(BASE_URL.'/user/dashboard');
				exit();
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	
	public function addUserApplication()
	{
		if($this->session->userdata('user_id') && $this->session->userdata('user_id') != '' && $this->session->userdata('user_logedin'))
		{
			$this->data['user_app_data'] = $this->input->post();
			
			if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'personal')
			{
				if(!$this->user_model->checkUserApplication($this->data['user_app_data']))
				{
					$this->data['user_personal'] = $this->user_model->insertUserFormDetails($this->data['user_app_data']);
					if($this->data['user_personal'])
					{
						$this->session->set_userdata('user_application_id', $this->data['user_personal']);
						$this->session->set_userdata('user_form_next', 'education');
						header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						redirect(BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						exit();
					}
				}
				else {
					$data['app_exists'] = 'You have already applied for this award';
					$data['result']=$this->user_model->getinfo();
					$data['query']=$this->user_model->getCurrentAwards();
					$data['country'] = $this->common_model->getAllCountry();
					$data['gender'] = $this->common_model->getGender();
					$data['form_list'] = $this->user_model->getFormList($value, 'Applicant Particulars');
					$data['form_list_edu'] = $this->user_model->getFormList($value, 'Educational Background');
					$data['form_list_doc'] = $this->user_model->getFormList($value, 'Document');
					$data['form_detail'] = $this->user_model->getFormData($value);
					if($data['form_list'] && $data['form_list'] != NULL)
					{
						$this->session->set_userdata('user_form_next', 'personal');
						$this->load->view('user/application_form',$data);
					}
					else {
						header('Location: '.BASE_URL.'/user/dashboard');
						redirect(BASE_URL.'/user/dashboard');
						exit();
					}
				}
			}
			else if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'education')
			{
				if(!$this->user_model->checkUserApplication($this->data['user_app_data']))
				{
					$this->session->set_userdata('fill_personal', 'Please fill in the personal details first');
					$this->session->set_userdata('user_form_next', 'personals');
					header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					redirect(BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					exit();
				}
				else{
					$this->data['user_personal'] = $this->user_model->insertUserFormDetails($this->data['user_app_data']);
					if($this->data['user_personal'])
					{
						$this->session->set_userdata('user_form_next', 'document');
						header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						redirect(BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						exit();
					}
					else{
						header('Location: '.BASE_URL.'/user/dashboard');
						redirect(BASE_URL.'/user/dashboard');
						exit();
					}
				}
			}
			else if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'document')
			{
				if(!$this->user_model->checkUserApplication($this->data['user_app_data']))
				{
					$this->session->set_userdata('fill_personal', 'Please fill in the personal details first');
					$this->session->set_userdata('user_form_next', 'personals');
					header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					redirect(BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					exit();
				}
				else{
					$this->data['user_app_data']['files'] = $_FILES;
					
					$this->data['user_personal'] = $this->user_model->insertUserFormDetails($this->data['user_app_data']);
					if($this->data['user_personal'])
					{
						if($this->data['user_app_data']['app_applicant_refree'] > 0){
							$this->session->set_userdata('user_form_next', 'refree');
							header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
							redirect(BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
							exit();	
						}
						else{
							$this->session->set_userdata('success_submit_app', 'Thankyou for submiting your application');
							$this->session->set_userdata('user_form_next', 'personal');
							header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
							redirect(BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
							exit();
						}
					}
					else{
						header('Location: '.BASE_URL.'/user/dashboard');
						redirect(BASE_URL.'/user/dashboard');
						exit();
					}
				}
			}
			else if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'refree')
			{
				if(!$this->user_model->checkUserApplication($this->data['user_app_data']))
				{
					$this->session->set_userdata('fill_personal', 'Please fill in the personal details first');
					$this->session->set_userdata('user_form_next', 'personals');
					header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					redirect(BASE_URL.'/user/application_new/'.$this->data['user_personal']['app_award_id']);
					exit();
				}
				else{
					//$this->data['user_personal'] = $this->user_model->insertUserFormDetails($this->data['user_app_data']);
					//var_dump($this->data['user_app_data']['refree_name']); die();
					$applicant_id = $this->data['user_app_data']['app_applicant_id'];
					$award_id = $this->data['user_app_data']['app_award_id'];
					$this->data['user_personal'] = $this->user_model->insertUserFormDetails($this->data['user_app_data']);
					$this->data['award_result'] = $this->user_model->getAwardDetail($award_id);
					$this->data['applicant_detail'] = $this->user_model->getApplicantDetail($applicant_id);
					$user_application_id = $this->data['applicant_detail'][0]['user_application_id'];//applicant id
					$applicant_name = $this->data['applicant_detail'][0]['user_application_field1'];
					if($this->data['applicant_detail'][0]['user_application_field2'] != NULL) {
						$applicant_name .=$this->data['applicant_detail'][0]['user_application_field2'];
					}
					$applicant_name .=$this->data['applicant_detail'][0]['user_application_field3'];
					$applicant_name = ucwords($applicant_name); 
					$award_name = ucfirst($this->data['award_result'][0]['award_name']);
					$r= $this->data['user_app_data'];
					$r_id = $this->data['user_personal'];
					$i = 0;
					foreach ($r['refree_email'] as $row){
						
						$refree_id = base64_encode($r_id[$i]);
						$config['protocol']='smtp';
						$config['smtp_host']='smtpout.asia.secureserver.net';
						$config['smtp_port']='80';
						$config['smtp_timeout']='30';
						$config['smtp_user']='info.elsevier@logiquebraincreations.com';
						$config['smtp_pass']='default@123';
						$config['charset']='utf-8';
						$config['newline']="\r\n";
						$config['wordwrap'] = TRUE;
						$config['mailtype'] = 'html';
						$this->email->initialize($config);
						$this->email->from('no-reply@elsevier.com', 'Elsevier');
						$this->email->to($row);
						$this->email->subject('Elsevier - Refree Review');
			       		$message='<!DOCTYPE html>
								<html>
									<head>
									<title>Elsevier</title>
									<meta charset="utf-8" />
									</head>
									
									<body style="margin:0px; padding:0px;">
									<table width="640" border="0" align="center" cellpadding="0" cellspacing="0"  style="background-color:#fafafa; border:1px solid #e1e1e1;">
									<tr bgcolor="#FF8200">
									<td><table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
									<tr>
									<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#ff8200" style="font-family:Calibri; font-size:16px; color:#000000;"><a href="http://www.elsevier.co.in" target="_blank" style="text-decoration:none"><img src="'.ASSET_URL.'img/logo.png" border="0" alt="Elsevier" style="display:block;" /></a></td>
									</tr>
									<tr>
									<td height="20" bgcolor="#ff8200"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									</table></td>
									</tr>
									<tr>
									<td>
									<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:#fafafa; margin:0 auto;">
									
									<tr>
									<td bgcolor="#fafafa">
									<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
									<tr>
									<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#FAFAFA" style="font-family:Calibri; font-size:16px; font-weight:bold; color:#333;">Dear '.ucwords($this->data['user_app_data']['refree_name'][$i]).',</td>
									</tr>
									</table>
									</td>
									</tr>
									<tr>
									<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333;">'.$applicant_name.' has recommended your refrence for the awards as a refree.</td>
									</tr>
									<tr>
									<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;">Please click on the  link below to Response.</td>
									</tr>
									<tr>
									<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;"><a href="'.BASE_URL.'/refree?key='.$refree_id.'" target="_blank" style="text-decoration:none; color:#ff8200;">Click here to response</a></td>
									</tr>
									<tr>
									<td height="30" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									<tr>
									<td bgcolor="#fafafa" style="font-family:Calibri; font-size:16px; color:#333; text-align:justify;">Regards,<br />
									Elsevier Awards</td>
									</tr>
									<tr>
									<td height="50" bgcolor="#FAFAFA"><span style="background-color:#fff;"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></span></td>
									</tr>
									</table>
									
									</td>
									</tr>
									<tr>
									<td bgcolor="#FFFFFF"><table width="640" border="0" align="center" cellpadding="0" cellspacing="0" style="vertical-align:top;">
									<tr>
									<td bgcolor="#232933"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;">
									<tr>
									<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
									</tr>
									<tr>
									<td valign="bottom" bgcolor="#232933" style="text-align:center;"><span style="font-size: 12px; font-family: Calibri; line-height: 17px; color: #ffffff; text-align: center;">Copyright &copy; 2015 Elsevier. All Rights Reserved </span></td>
									</tr>
									<tr>
									<td height="20" bgcolor="#232933"><img style="display:block;" src="'.ASSET_URL.'img/spacer.gif" alt="" width="1" height="1" border="0" /></td>
									</tr>
									</table></td>
									</tr>
									</table></td>
									</tr>
									</table>
								</body>
						</html>';
					$this->email->message($message);
					$this->email->send();	
					$i++;}
					$this->email->send();
					if($this->data['user_personal'])
					{
						$this->session->set_userdata('success_submit_app', 'Thankyou for submiting your application');
						$this->session->set_userdata('user_form_next', 'personal');
						header('Location: '.BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						redirect(BASE_URL.'/user/application_new/'.$this->data['user_app_data']['app_award_id']);
						exit();
					}
					else{
						header('Location: '.BASE_URL.'/user/dashboard');
						redirect(BASE_URL.'/user/dashboard');
						exit();
					}
				}
			}
			else if($data['form_list'] && $data['form_list'] != NULL)
			{
				$this->load->view('user/application_form',$data);
			}
			else {
				header('Location: '.BASE_URL.'/user/dashboard');
				redirect(BASE_URL.'/user/dashboard');
				exit();
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	
	public function nominationNew()
	{
		$event = $this->input->get('event');
		$event_id = base64_decode($event);
		$award = $this->input->get('award');
		$award_id = base64_decode($award);
		if(isset($event_id) && $event_id != '' && preg_match("/^[0-9]+$/",$event_id) && isset($award_id) && $award_id != '' && preg_match("/^[0-9]+$/",$award_id))
		{
			if($this->user_model->validateEventAward($event_id, $award_id))
			{	
				$data['query']=$this->user_model->validateEventAward($event_id, $award_id);
				$data['country'] = $this->common_model->getAllCountry();
				$data['gender'] = $this->common_model->getGender();
				$data['form_list'] = $this->user_model->getNominationFormList($event_id, 'Nominee');
				$data['form_list_nom'] = $this->user_model->getNominationFormList($event_id, 'Nominating Party');
				$data['form_list_dis'] = $this->user_model->getNominationFormList($event_id, 'Disclosure Notice');
				$data['form_detail'] = $this->user_model->getFormData($award_id);
				//$data['form_submit'] = $this->user_model->checkFormSubmit($value, $this->session->userdata['user_id']);
				if($data['form_list'] && $data['form_list'] != NULL)
				{
					if($this->session->userdata('user_form_next') && $this->session->userdata('user_form_next') != '')
					{
						$this->session->unset_userdata('user_form_next1');
					}
					else {
						$this->session->set_userdata('user_form_next1', 'nominating');
					}
					
					$this->load->view('user/nomination_form',$data);
				}
			}
			else{
				$this->session->sess_destroy();
				header('Location: '.BASE_URL.'/login');
				redirect(BASE_URL.'/login');
				exit();
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}

	public function addUserNomination()
	{
			$this->data['user_app_data'] = $this->input->post();
			
			if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'nominating')
			{
				$this->data['user_personal'] = $this->user_model->insertUserNominationFormDetails($this->data['user_app_data']);
				if($this->data['user_personal'])
				{
					$this->session->set_userdata('user_nomination_id', $this->data['user_personal']);
					$this->session->set_userdata('user_form_next', 'nominee');
					header('Location: '.BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					redirect(BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					exit();
				}
			}
			else if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'nominee')
			{
				$this->data['user_personal'] = $this->user_model->insertUserNominationFormDetails($this->data['user_app_data']);
				if($this->data['user_personal'])
				{
					$this->session->set_userdata('user_form_next', 'disclosure');
					header('Location: '.BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					redirect(BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					exit();
				}
				else{
					header('Location: '.BASE_URL.'/user/dashboard');
					redirect(BASE_URL.'/user/dashboard');
					exit();
				}
			}
			else if(isset($this->data['user_app_data']['app_type']) && $this->data['user_app_data']['app_type'] == 'disclosure')
			{
				$this->data['user_personal'] = $this->user_model->insertUserNominationFormDetails($this->data['user_app_data']);
				if($this->data['user_personal'])
				{
					$this->session->set_userdata('user_form_next', 'nominating');
					$this->session->set_userdata('user_form_complete', 'Thankyou for submitting your nomination');
					header('Location: '.BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					redirect(BASE_URL.'/user/nomination?event='.base64_encode($this->data['user_app_data']['app_event_id']).'&award='.base64_encode($this->data['user_app_data']['app_award_id']));
					exit();
				}
				else{
					header('Location: '.BASE_URL.'/user/dashboard');
					redirect(BASE_URL.'/user/dashboard');
					exit();
				}
			}
			else {
				header('Location: '.BASE_URL);
				redirect(BASE_URL);
				exit();
			}
	}	
}
?>